<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Exception;
use think\Db;

class Notebook extends Base
{
    // 记事本列表
    public function notebookList()
    {
        $data = input();//manage_id rows page
        $data['manage_id'] = $this->username;
        $where['manage_id'] = $data['manage_id'];
        if (isset($data['title'])) {
            if (!empty($data['title'])) {
                $where['title'] = array('like','%'.$data['title'].'%');
            }
        }
        $list = db('crm_notebook')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('zhiding DESC,sort ASC,date DESC')->select();

        $count = db('crm_notebook')->where($where)->count();

        foreach ($list as $key => &$value) {
            $value['date'] = date("Y-m-d",$value['date']);
            $value['notebook_next'] = db('crm_notebook_next')->where('notebook_id',$value['id'])->order('sort_order ASC,id ASC')->select();
            foreach ($value['notebook_next'] as $key1 => &$value1) {
                $time = $value1['time'];
                if ($time < time()) {
                    $value1['time_status'] = 3;//已过期
                }elseif (($time-time()) <= 10*24*3600) {
                    $value1['time_status'] = 2;//10天内
                }else{
                    $value1['time_status'] = 1;//安全范围
                }
            }
        }

        apiReturn(200,'获取成功',['list'=>$list,'count'=>$count]);
    }

    // 新增修改记事本
    public function notebookAdd()
    {
        $data = input();//manage_id id title sort zhiding notebook_next(name,time,sort_order)
        if (!isset($data['manage_id']) || !isset($data['title']) || !isset($data['sort']) || !isset($data['notebook_next'])) {
            apiReturn(201,'参数错误');
        }
        Db::startTrans();
        try{
            $notebook_datas = [
                'manage_id'=>$data['manage_id'],
                'title'=>$data['title'],
                'sort'=>$data['sort'],
                'zhiding'=>$data['zhiding'],
                'date'=>time()
            ];
            if (isset($data['id'])) {
                if (db('crm_notebook')->where('id',$data['id'])->update($notebook_datas)) {
                    db('crm_notebook_next')->where('notebook_id',$data['id'])->delete();
                    $notebook_next_datas = [];
                    foreach ($data['notebook_next'] as $key => $value) {
                        $time = (!empty($value['time'])) ? strtotime($value['time']) : null;
                        $notebook_next_datas[] = [
                            'notebook_id'=>$data['id'],
                            'name'=>$value['name'],
                            'time'=>$time,
                            'sort_order'=>$value['sort_order'],
                            'date'=>time()
                        ];
                    }
                    if ($numbers = db('crm_notebook_next')->insertAll($notebook_next_datas)) {
                        Db::commit();
                        apiReturn(200,'提交成功');
                    }else{
                        Db::rollback();
                        apiReturn(201,'修改失败');
                    }
                }else{
                    Db::rollback();
                    apiReturn(201,'修改失败');
                }
            }else{
                if ($id = db('crm_notebook')->insertGetId($notebook_datas)) {
                    $notebook_next_datas = [];
                    foreach ($data['notebook_next'] as $key => $value) {
                        $time = (!empty($value['time'])) ? strtotime($value['time']) : null;
                        $notebook_next_datas[] = [
                            'notebook_id'=>$id,
                            'name'=>$value['name'],
                            'time'=>$time,
                            'sort_order'=>$value['sort_order'],
                            'date'=>time()
                        ];
                    }
                    if ($numbers = db('crm_notebook_next')->insertAll($notebook_next_datas)) {
                        Db::commit();
                        apiReturn(200,'提交成功');
                    }else{
                        Db::rollback();
                        apiReturn(201,'添加失败');
                    }
                }else{
                    Db::rollback();
                    apiReturn(201,'添加失败');
                }
            }
        }catch (\Exception $e){
            Db::rollback();
            apiReturn(400,$e->getMessage());
        }
    }

    // 删除记事本
    public function notebookDelete()
    {
        $data = input();//id
        if (!isset($data['id'])) {
            apiReturn(201,'参数错误');
        }
        if (db('crm_notebook')->where('id',$data['id'])->delete()) {
            db('crm_notebook_next')->where('notebook_id',$data['id'])->delete();
            apiReturn(200,'删除成功');
        }else{
            apiReturn(201,'删除失败');
        }
    }

}
?>